<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="/css/common.css">
    <link rel="stylesheet" href="/css/register.css">
</head>
<body>
    <div class="nav">
        五子棋
    </div>
    <div class="register-container">
        <div class="register-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id = "name">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <button id = "submit">注册</button>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
          let btn = document.querySelector('#submit');
          let name = $('#name').val();
            let password = $("#password").val();
            if (name == null) {
                alert("用户名不能为空");
               // return;
            }
            if (password == null) {
                alert("用户密码不能为空");
                return;
            }
          btn.onclick = function() {
            $.ajax({
                url: "/register",
                type: "POST",
                contentType: 'application/json',
                data: JSON.stringify({
                    name: $("#name").val(),
                    password: $("#password").val(),
                }),
                success: function(result) {
                    if(result.code == 200) {
                        // 注册成功，跳转至登录页面
                        location.assign("login.html");
                    }else {
                        alert(result.errorMessage);
                    }
                }
            });
          }
    </script>
</body>
</html>